package com.xzp.blog.controller;

import com.xzp.blog.controller.base.BaseController;
import com.xzp.blog.entity.About;
import com.xzp.blog.entity.dto.form.AboutModifyForm;
import com.xzp.blog.service.IAboutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static com.xzp.blog.consts.ViewConsts.VIEW_ARTICLE;

@Controller
public class AboutController extends BaseController{

    @Autowired
    private IAboutService aboutService;

//    @ModelAttribute
//    public About createAbout(){
//        return new About();
//    }

    @GetMapping("/about")
    public String getAbout(HttpServletRequest request,Model model) throws Exception {
        About about = aboutService.getAbout();
        HttpSession session = request.getSession();
//        session.setAttribute("article",about);
        addModelAtt(model,VIEW_ARTICLE,about);
//        model.addAttribute("article",about);
        return "abouts";
//        return "posts";
    }

    @PostMapping("/update_about.f")
    @ResponseBody
    public Object updateAbout(AboutModifyForm form,@ModelAttribute(binding = false) About about){
        aboutService.updateAbout(form,about);
        return responseSimpleOK();
    }
}
